﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace TSComm.Utilities.Sys
{
    /// <summary>
    /// 用户事件类
    /// </summary>
    public class SysEvent
    {
        private string StrSource { get; set; }
        private bool eventLogCreateSuccess = false;

        public SysEvent():this("Unknown Application")
        { 
        }

        public SysEvent(string strSource)
        {
            StrSource = strSource;
            try
            {
                if (!EventLog.SourceExists(StrSource))
                {
                    EventLog.CreateEventSource(StrSource, "Event Source Create.");
                    eventLogCreateSuccess = true;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
           
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="strEvnetLog"></param>
        /// <param name="entryType"></param>
        /// <param name="eventId"></param>
        public void WriteEvent(string strEvnetLog, EventLogEntryType entryType, int eventId = 0)
        {
            if(eventLogCreateSuccess)
                EventLog.WriteEntry(StrSource, strEvnetLog, entryType, eventId);
        }

        /// <summary>
        /// 写入Information类型日志
        /// </summary>
        /// <param name="strEventLog"></param>
        /// <param name="eventId"></param>
        public void WriteEventInformation(string strEventLog, int eventId = 0)
        {
            if(eventLogCreateSuccess)
                EventLog.WriteEntry(StrSource, strEventLog, EventLogEntryType.Information, eventId);
        }

        /// <summary>
        /// 写入Warring类型日志
        /// </summary>
        /// <param name="strEventLog"></param>
        /// <param name="eventId"></param>
        public void WriteEventWarring(string strEventLog, int eventId = 0)
        {
            if(eventLogCreateSuccess)
                EventLog.WriteEntry(StrSource, strEventLog, EventLogEntryType.Warning, eventId);
        }

        /// <summary>
        /// 写入Error类型日志
        /// </summary>
        /// <param name="strEventLog"></param>
        /// <param name="eventId"></param>
        public void WriteEventError(string strEventLog, int eventId = 0)
        {
            if(eventLogCreateSuccess)
                EventLog.WriteEntry(StrSource, strEventLog, EventLogEntryType.Error, eventId);
        }

    }
}
